Add an accessor for grid children
authorMatthias Clasen <mclasen@redhat.com>
Mon, 29 Aug 2011 01:20:53 +0000 (21:20 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 29 Aug 2011 01:20:53 +0000 (21:20 -0400)
This addresses concerns in bug 634136.

docs/reference/gtk/gtk3-sections.txt
gtk/gtk.symbols
gtk/gtkgrid.c
gtk/gtkgrid.h
tests/testgrid.c

index 2cc0722e57d654926f8e803aaf253b42ee593c56..aca18e9a2d6dc7763897b3d4fefe44b18ed8072c 100644 (file)
@@ -6988,6 +6988,7 @@ GtkGrid
 gtk_grid_new
 gtk_grid_attach
 gtk_grid_attach_next_to
+gtk_grid_get_child_at
 gtk_grid_insert_row
 gtk_grid_insert_column
 gtk_grid_insert_next_to
index 9a42c010745bece6e010268b0055b317f94e84e0..8fe844a621abc879c2d75cac08a3b7c436999b94 100644 (file)
@@ -1114,6 +1114,7 @@ gtk_grab_get_current
 gtk_grab_remove
 gtk_grid_attach
 gtk_grid_attach_next_to
+gtk_grid_get_child_at
 gtk_grid_get_column_homogeneous
 gtk_grid_get_column_spacing
 gtk_grid_get_row_homogeneous
index 20ec6882948ae3921e7ffa850ca23d1fd09ccf54..cfb21f41ba54014926e4f6365a2ab799e05ff152 100644 (file)
@@ -1479,6 +1479,42 @@ gtk_grid_attach_next_to (GtkGrid         *grid,
   grid_attach (grid, child, left, top, width, height);
 }
 
+/**
+ * gtk_grid_get_child_at:
+ * @grid: a #GtkGrid
+ * @left: the left edge of the cell
+ * @top: the top edge of the cell
+ *
+ * Gets the child of @grid whose area covers the grid
+ * cell whose upper left corner is at @left, @top.
+ *
+ * Returns: the child at the given position, or %NULL
+ *
+ * Since: 3.2
+ */
+GtkWidget *
+gtk_grid_get_child_at (GtkGrid *grid,
+                       gint     left,
+                       gint     top)
+{
+  GtkGridPrivate *priv = grid->priv;
+  GtkGridChild *child;
+  GList *list;
+
+  for (list = priv->children; list; list = list->next)
+    {
+      child = list->data;
+
+      if (CHILD_LEFT (child) <= left &&
+          CHILD_LEFT (child) + CHILD_WIDTH (child) > left &&
+          CHILD_TOP (child) <= top &&
+          CHILD_TOP (child) + CHILD_HEIGHT (child) > top)
+        return child->widget;
+    }
+
+  return NULL;
+}
+
 /**
  * gtk_grid_insert_row:
  * @grid: a #GtkGrid
index 99f9e4e300d1f07992578114c2af546da335d70b..491ba2232198cff47d6fd2aba7cf653c5e5f3168 100644 (file)
@@ -79,6 +79,9 @@ void       gtk_grid_attach_next_to         (GtkGrid         *grid,
                                             GtkPositionType  side,
                                             gint             width,
                                             gint             height);
+GtkWidget *gtk_grid_get_child_at           (GtkGrid         *grid,
+                                            gint             left,
+                                            gint             top);
 void       gtk_grid_insert_row             (GtkGrid         *grid,
                                             gint             position);
 void       gtk_grid_insert_column          (GtkGrid         *grid,
index cb79b359f7326fade20be178feb524465468a95f..c756158f9281677d57ca50e85024f3eafbd529ca 100644 (file)
@@ -40,7 +40,7 @@ simple_grid (void)
 {
   GtkWidget *window;
   GtkWidget *grid;
-  GtkWidget *child;
+  GtkWidget *test1, *test2, *test3, *test4, *test5, *test6;
 
   window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
   gtk_window_set_title (GTK_WINDOW (window), "Orientation");
@@ -50,17 +50,28 @@ simple_grid (void)
 
   gtk_grid_set_column_spacing (GTK_GRID (grid), 5);
   gtk_grid_set_row_spacing (GTK_GRID (grid), 5);
-  gtk_container_add (GTK_CONTAINER (grid), test_widget ("1", "red"));
-  gtk_container_add (GTK_CONTAINER (grid), test_widget ("2", "green"));
-  gtk_container_add (GTK_CONTAINER (grid), test_widget ("3", "blue"));
-  child = test_widget ("4", "green");
-  gtk_grid_attach (GTK_GRID (grid), child, 0, 1, 1, 1);
-  gtk_widget_set_vexpand (child, TRUE);
-  gtk_grid_attach_next_to (GTK_GRID (grid), test_widget ("5", "blue"), child, GTK_POS_RIGHT, 2, 1);
-  child = test_widget ("6", "yellow");
-  gtk_grid_attach (GTK_GRID (grid), child, -1, 0, 1, 2);
-  gtk_widget_set_hexpand (child, TRUE);
-
+  test1 = test_widget ("1", "red");
+  gtk_container_add (GTK_CONTAINER (grid), test1);
+  test2 = test_widget ("2", "green");
+  gtk_container_add (GTK_CONTAINER (grid), test2);
+  test3 = test_widget ("3", "blue");
+  gtk_container_add (GTK_CONTAINER (grid), test3);
+  test4 = test_widget ("4", "green");
+  gtk_grid_attach (GTK_GRID (grid), test4, 0, 1, 1, 1);
+  gtk_widget_set_vexpand (test4, TRUE);
+  test5 = test_widget ("5", "blue");
+  gtk_grid_attach_next_to (GTK_GRID (grid), test5, test4, GTK_POS_RIGHT, 2, 1);
+  test6 = test_widget ("6", "yellow");
+  gtk_grid_attach (GTK_GRID (grid), test6, -1, 0, 1, 2);
+  gtk_widget_set_hexpand (test6, TRUE);
+
+  g_assert (gtk_grid_get_child_at (GTK_GRID (grid), 0, -1) == NULL);
+  g_assert (gtk_grid_get_child_at (GTK_GRID (grid), 0, 0) == test1);
+  g_assert (gtk_grid_get_child_at (GTK_GRID (grid), 1, 0) == test2);
+  g_assert (gtk_grid_get_child_at (GTK_GRID (grid), 0, 1) == test4);
+  g_assert (gtk_grid_get_child_at (GTK_GRID (grid), -1, 0) == test6);
+  g_assert (gtk_grid_get_child_at (GTK_GRID (grid), -1, 1) == test6);
+  g_assert (gtk_grid_get_child_at (GTK_GRID (grid), -1, 2) == NULL);
   gtk_widget_show_all (window);
 }